﻿using Application.Interfaces.Repository;
using Domain.Core.Attributes;
using System.Data.Entity;
using System.Linq;

namespace Infrastructure.Implementations.Repository.EntityFramework
{
    public class MyDbContext : DbContext
    {
        public MyDbContext(IRepositoryProvider repositoryProvider)
            : base(repositoryProvider.ConnectionString)
        {
#if Debug
            this.Database.Log = l => ApplicationEngine.Container.GetInstance<ILogger>().Log(l);
#endif
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);
            modelBuilder.Properties().Where(o => o.CustomAttributes.Any(p => p.AttributeType == typeof(TransactionAttribute))).Configure(o => o.IsRowVersion());
        }
    }
}